package com.shenqi.language.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.shenqi.language.converter.UservipVOConverter;
import com.shenqi.language.domain.InvitationCode;
import com.shenqi.language.service.IInvitationCodeService;
import com.shenqi.language.vo.UservipVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.shenqi.language.domain.Uservip;
import com.shenqi.language.service.IUservipService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * VIP信息Controller
 * 
 * @author shenqi
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/language/uservip")
public class UservipController extends BaseController
{
    @Autowired
    private IUservipService uservipService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IInvitationCodeService invitationCodeService;

    /**
     * 查询VIP信息列表
     */
    @PreAuthorize("@ss.hasPermi('language:uservip:list')")
    @GetMapping("/list")
    public TableDataInfo list(Uservip uservip)
    {
        startPage();
        List<Uservip> list = uservipService.selectUservipList(uservip);
        return getDataTable(list);
    }

    /**
     * 导出VIP信息列表
     */
    @PreAuthorize("@ss.hasPermi('language:uservip:export')")
    @Log(title = "VIP信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Uservip uservip)
    {
        List<Uservip> list = uservipService.selectUservipList(uservip);
        ExcelUtil<Uservip> util = new ExcelUtil<Uservip>(Uservip.class);
        util.exportExcel(response, list, "VIP信息数据");
    }

    /**
     * 获取VIP信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('language:uservip:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(uservipService.selectUservipByUserId(userId));
    }

    /**
     * 新增VIP信息
     */
    @PreAuthorize("@ss.hasPermi('language:uservip:add')")
    @Log(title = "VIP信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Uservip uservip)
    {
        return toAjax(uservipService.insertUservip(uservip));
    }

    /**
     * 修改VIP信息
     */
    @PreAuthorize("@ss.hasPermi('language:uservip:edit')")
    @Log(title = "VIP信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Uservip uservip)
    {
        return toAjax(uservipService.updateUservip(uservip));
    }

    /**
     * 删除VIP信息
     */
    @PreAuthorize("@ss.hasPermi('language:uservip:remove')")
    @Log(title = "VIP信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(uservipService.deleteUservipByUserIds(userIds));
    }

    @Anonymous
    @PostMapping("/useInvitationCode")
    public AjaxResult remove(@RequestParam String invitationCode, @RequestParam String username)
    {
        if(StringUtils.isEmpty(invitationCode)) {
            return AjaxResult.error("激活码不能为空");
        }
        if(StringUtils.isEmpty(username)) {
            return AjaxResult.error("用户账号不能为空");
        }
        InvitationCode code = invitationCodeService.selectInvitationCodeByCode(invitationCode);
        if(code == null || code.getUserId() != null) {
            return AjaxResult.error("激活码无效或已使用");
        }

        SysUser user = sysUserService.selectUserByUserName(username);
        if(user == null) {
            return AjaxResult.error("用户账号不存在，请注意检查");
        }
        uservipService.useInvitationCode(invitationCode, user.getUserId());
        return success();
    }

    /**
     * 返回当前登录用户的VIP信息。
     * @return
     */
    @GetMapping("getMyVipInfo")
    public AjaxResult getMyVipInfo()
    {
        Long userId = SecurityUtils.getUserId();
        Uservip vip = uservipService.selectUservipByUserId(userId);
        UservipVO vipVo = UservipVOConverter.INSTANCE.toVO(vip);
        return success(vipVo);
    }
}
